﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using TA.DotNetUtils;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all <see cref="Repeater"/> page buttons.
	/// </summary>
	public abstract class BaseRepeaterPageButton : BaseRepeaterControl
	{
		internal BaseRepeaterPageButton(Repeater repeater)
			: base(repeater)
		{
		}
		/// <summary>
		/// Renders the button.
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderControl(HtmlTextWriter writer)
		{
			int displayIndex = this.Index + 1;

			string saveText = this.PagerElement.Text,
				saveImageSrc = this.PagerElement.ImageSrc,
				saveImageAlt = this.PagerElement.ImageAlt;

			this.PagerElement.Text = (this.PagerElement.Text ?? "").Replace("###", displayIndex.ToString());
			this.PagerElement.ImageSrc = (this.PagerElement.ImageSrc ?? "").Replace("###", displayIndex.ToString());
			this.PagerElement.ImageAlt = (this.PagerElement.ImageAlt ?? "").Replace("###", displayIndex.ToString());

			this.AddAttributesToRender(writer);
			writer.RenderBeginTag(HtmlTextWriterTag.A);

			this.PagerElement.Render(writer);

			writer.RenderEndTag();




			this.PagerElement.Text = saveText;
			this.PagerElement.ImageSrc = saveImageSrc;
			this.PagerElement.ImageAlt = saveImageAlt;
		}

		/// <summary>
		/// Adds attributes for this button (href, class and style).
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (!this.Selected)
			{
				string href = this.PagerElement.Href, hrefJs = this.PagerElement.HrefJS;
				if (string.IsNullOrEmpty(href))
					href = this.PagerSettings.ButtonHref;
				if (string.IsNullOrEmpty(hrefJs))
					hrefJs = this.PagerSettings.ButtonHrefJS;
				if (!string.IsNullOrEmpty(href) || !string.IsNullOrEmpty(hrefJs))
				{
					Link.AddHrefAttribute(
						writer,
						this,
						(href ?? "").Replace("###", (this.Index + 1).ToString()),
						(hrefJs ?? "").Replace("###", (this.Index + 1).ToString()));
				}
				else
					writer.AddAttribute(HtmlTextWriterAttribute.Href, " ");
			}
			if (!(this.PagerElement.Visible))
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
			if (!string.IsNullOrEmpty(this.CssClass))
				writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
		}
		/// <summary>
		/// Gets <see cref="Repeater.Pager"/>.
		/// </summary>
		public PagerSettings PagerSettings
		{
			get
			{
				return this.Repeater.Pager;
			}
		}
		/// <summary>
		/// Gets true if this button is selected and shouldn't be clickable, otherwise false.
		/// </summary>
		public virtual bool Selected
		{
			get
			{
				return this.Index == this.Repeater.ActualPageIndex;
			}
		}
		/// <summary>
		/// When implemented, gets the <see cref="PagerElement"/> appropriate for this type of page button.
		/// </summary>
		public abstract PagerElement PagerElement { get; }
		/// <summary>
		/// When implemented, gets the index of this button.
		/// </summary>
		public abstract int Index { get; }
		/// <summary>
		/// When implemented, gets the CSS class for this button.
		/// </summary>
		public abstract string CssClass { get; }
	}

}